package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.statistics.LoanAdsChannelDetail;
import com.tuanzi.loan.business.repository.statistics.LoanAdsChannelDetailRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanAdsChannelDetail.LOAN_ADS_CHANNEL_DETAIL;


/**
 * @author Simon
 * @date 2019/07/29 14:28
 */
@Repository
public class LoanAdsChannelDetailRepositoryImpl extends JOOQBaseRepository implements LoanAdsChannelDetailRepository {

    protected TableImpl getTable() {
        return LOAN_ADS_CHANNEL_DETAIL;
    }
    public void updateAccessToken(String channelCode, String accessToken) {
        dslContext.update(LOAN_ADS_CHANNEL_DETAIL).set(LOAN_ADS_CHANNEL_DETAIL.ACCESS_TOKEN,accessToken).where(LOAN_ADS_CHANNEL_DETAIL.CHANNEL_CODE.eq(channelCode)).execute();
    }

    public void insert(LoanAdsChannelDetail loanAdsChannelDetail) {
        dslContext.insertInto(LOAN_ADS_CHANNEL_DETAIL).values(loanAdsChannelDetail).execute();
    }

    public List<LoanAdsChannelDetail> findAll(Integer adsPlatformType) {
        if(adsPlatformType < 1){
            return dslContext.select(LOAN_ADS_CHANNEL_DETAIL.fields()).from(LOAN_ADS_CHANNEL_DETAIL).fetchInto(LoanAdsChannelDetail.class);
        }else {
            return dslContext.select(LOAN_ADS_CHANNEL_DETAIL.fields()).from(LOAN_ADS_CHANNEL_DETAIL).where(LOAN_ADS_CHANNEL_DETAIL.ADS_PLATFORM_TYPE.eq(adsPlatformType)).fetchInto(LoanAdsChannelDetail.class);
        }
    }
    public List<LoanAdsChannelDetail> findAllChannel() {
        return dslContext.select(LOAN_ADS_CHANNEL_DETAIL.fields()).from(LOAN_ADS_CHANNEL_DETAIL).fetchInto(LoanAdsChannelDetail.class);
    }

    public LoanAdsChannelDetail findByChannel(String channelCode) {
        return dslContext.select(LOAN_ADS_CHANNEL_DETAIL.fields()).from(LOAN_ADS_CHANNEL_DETAIL).where(LOAN_ADS_CHANNEL_DETAIL.CHANNEL_CODE.eq(channelCode)).fetchOneInto(LoanAdsChannelDetail.class);
    }

}
